package hash_map;

import java.util.HashMap;
import java.util.Map;

//两数之和
public class leetcode1 {
    public static void main(String[] args) {
        int[] nums = {3,2,4};
        int target = 6;
        int[] result = twoSum(nums, target);
        if(result != null){
            System.out.println("[" + result[0] + "," + result[1] + "]");
        }
    }

    public static int[] twoSum(int[] nums, int target) {

        int[] res = new int[2];
        if(nums.length <= 1){return null;}

        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if(map.containsKey(temp)){
                res[0] = i;
                res[1] = map.get(temp);
                return res;
            }
            map.put(nums[i], i);
        }

        return null;
    }
}
